package com.young.msg.event;

import com.young.common.core.event.IEventSubscriber;
import com.young.common.util.CollectionUtil;
import com.young.common.util.DateUtil;
import com.young.common.util.JsonUtil;
import com.young.interfaces.common.event.model.EventMessage;
import com.young.interfaces.common.event.model.IEvent;
import com.young.interfaces.log.annotation.Log;
import com.young.interfaces.ums.model.event.EUmsEventDefinitionEnum;
import com.young.interfaces.ums.model.event.LoginSuccessEvent;
import com.young.msg.model.SiteLetter;
import com.young.msg.model.SiteLetterReceive;
import com.young.msg.service.ISiteLetterReceiveService;
import com.young.msg.service.ISiteLetterService;
import com.young.msg.util.MsgPojoFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 登录成功事件订阅
 *
 * @author wangjy
 * @date 2021/11/13 14:37:13
 */
@Service
public class LoginSuccessSubscriber implements IEventSubscriber<LoginSuccessEvent> {

    private static final Logger logger = LoggerFactory.getLogger(LoginSuccessSubscriber.class);

    /**
     * 收信服务
     */
    @Autowired
    private ISiteLetterReceiveService siteLetterReceiveService;
    /**
     * 站内信服务
     */
    @Autowired
    private ISiteLetterService siteLetterService;

    /**
     * 订阅者名字
     *
     * @return
     */
    @Override
    public String name() {
        return "com.young.msg.event.LoginSuccessSubscriber";
    }

    /**
     * 订阅的事件
     *
     * @return 订阅的事件
     */
    @Override
    public IEvent[] events() {
        return new IEvent[]{EUmsEventDefinitionEnum.LOGIN_SUCCESS};
    }

    /**
     * 事件处理
     *
     * @param message 事件消息
     */
    @Log("用户登录后站内信处理")
    @Override
    public void process(EventMessage<LoginSuccessEvent> message) {
        try {
            LoginSuccessEvent event = message.getData();
            logger.info("[登录成功事件处理-站内信] 收到登录消息={}", JsonUtil.toJson(event));
            // 收信
            siteLetterReceiveService.receiveLetter(event.getUserId());
        } catch (Throwable t) {
            logger.error("[登录成功事件处理-站内信] 收取站内信发生异常,事件对象={}", JsonUtil.toJson(message), t);
        }
    }


}
